jQuery(document).ready(function() {
	
	//Fieldsets Minimizáveis
	$('fieldset').addClass('collapsible');
	$('fieldset.collapsible').collapse();
	
	$('select.autocomplete').select_autocomplete();
	
	$('input.maskData:enabled').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd/mm/yy', showOn: 'button', buttonImage: '../../template/img/calendar.png', buttonImageOnly: true
	});
	$('input.maskData:enabled').datepicker($.datepicker.regional['pt-BR']);
	
	$('input.maskMoeda').setMask({mask : '99,999.999.99', type : 'reverse', defaultValue: '000'});
	$('input.maskCnpj').setMask('cnpj');
	$('input.maskCpf').setMask('cpf');
	$('input.maskCep').setMask('cep');
	$('input.maskNumeros').setMask({mask:'9', type:'repeat'});
	$('input.maskData').setMask('date');
	$('input.maskFone').setMask('phone');
	$('input[name=txtDiasVigencia]').setMask({mask:'9', type:'repeat', maxLength:4});
	$('input[name=txtPrazoAvisoPrevio]').setMask({mask:'9', type:'repeat', maxLength:3});
	$('input.maskNumeroLicitacao').setMask({mask: '9999/9999'});
	$('input:text').addClass('required');
	$('textarea').addClass('required');
	$('select').addClass('required');
	$('select.opcional').removeClass('required');
	$('input.opcional').removeClass('required');
	$('textarea.opcional').removeClass('required');
	
	// Função para selecionar uma linha do grid
	$('.grid tbody tr td input[type=checkbox]').click(
		function(){
			$(this).parent().parent().toggleClass('selected');
		}
	);
	
	// Função para, quando clicar em uma linha do grid, ir para o link dela, se existir.
	/*$('.grid tbody tr').click(
		function(){
			document.location = $('td a', this).attr('href');
		}
	);*/
	
	// Bruno: tá aqui a solução claudemirton:
	$('form').each(function(intIndex){ 
		$(this).validate();
	});
	
	// Script para ativar a verificacao de cpf da tela de historico
	if ( document.getElementById( 'txtCpf' ) && document.getElementById( 'nomeOriginal' ) && document.getElementById( 'contatoOriginal' ) )
		verificarAlteracaoCpf();
	
	// Botão Default - Enter
	$('form input, form select').live('keypress', function (e) {
		if ($(this).parents('form').find('button[type=submit].default, input[type=submit].default').length <= 0)
		return true;
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
		$(this).parents('form').find('button[type=submit].default, input[type=submit].default').click();
		return false;
		} else {
		return true;
		}
	});
	
});

function Horas()
{
    var t;
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();

    var day   = now.getDate();
    var month = now.getMonth()+1;
    var year  = now.getFullYear();

    if (hours <=9)
    { hours="0"+hours; }
    if (minutes<=9)
    { minutes="0"+minutes; }
    if (seconds<=9)
    { seconds="0"+seconds; }

    if (day <=9)
    { day="0"+day; }
    if (month <=9)
    { month="0"+month; }

    var cdate=day+"/"+month+"/"+year+" "+hours+":"+minutes+":"+seconds;
    document.getElementById("clock").innerHTML = cdate;
    t = setTimeout("Horas()",1000);
}

function VerificaSelecao()
{
    var obj = document.getElementsByTagName("input");
    var cont = 0;
    for (var i=0; i < obj.length; i++) {
        if (obj[i].type == "checkbox")
        if(obj[i].checked == true) cont++;
    }
    if (cont == 0)
    { 
        alert("você deve marcar ao menos 1 checkbox.");
        return false;
    }
    if (cont > 1) 
    {
        alert("você só deve editar 1 registro de cada vez.");
        return false;
    }
    return true;
}

function checkedAll (id, checked)
{
    var el = document.getElementsByName(id);
    for (var i = 0; i <el.length; i++)
    { el[i].checked = checked; }
    
    if (checked == true ) {
	    if ($('.grid tbody tr.selected').removeClass('selected')) {
	    	$('.grid tbody tr').addClass('selected');
	    }
    }
    else {
    	$('.grid tbody tr.selected').removeClass('selected');
	}
}

function verificarAlteracaoCpf(){
	var object = document.getElementById( 'txtCpf' );
	var cpf = document.getElementById( 'cpfOriginal' ).firstChild.nodeValue;
	var nome = document.getElementById( 'nomeOriginal' ).firstChild.nodeValue;
	var contato = document.getElementById( 'contatoOriginal' ).firstChild.nodeValue;
	var telefone2 = document.getElementById( 'telefone2Original' ).firstChild.nodeValue;
	var cpfnovo = object.value;
	if ( cpf == cpfnovo ){
		document.getElementById( 'txtNome' ).disabled = true;
		document.getElementById( 'txtContato' ).disabled = true;
		document.getElementById( 'txtNome' ).value = nome;
		document.getElementById( 'txtContato' ).value = contato;
		document.getElementById( 'txtTelefone2' ).value = contato;
	} else {
		document.getElementById( 'txtNome' ).disabled = false;
		document.getElementById( 'txtContato' ).disabled = false;
		document.getElementById( 'txtNome' ).value = '';
		document.getElementById( 'txtContato' ).value = '';
		document.getElementById( 'txtTelefone2' ).value = telefone2;
	}
}

function verificarFirebug()
{
	if (location.href.indexOf("firebug") < 0 ){
    	if(document.getElementById("_firebugConsole")) {
            window.location.href = '../../view/home/index.php?method=firebug';
        }
	}
	setTimeout("verificarFirebug()",10000);
}